//
//  LoginViewController.m
//  yemeishe1
//
//  Created by 吴坤金 on 2017/2/28.
//  Copyright © 2017年 yameishe. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginTranslation.h"
#import "RegisterViewController.h"
#import "TabBarViewController.h"


#import "YMSMemberManager.h"
@interface LoginViewController ()<UIViewControllerTransitioningDelegate>
@property (weak, nonatomic) IBOutlet UIButton *btn;
@property (weak, nonatomic) IBOutlet UITextField *userNameTextF;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextF;
@property (nonatomic, strong) LoginTranslation *login;
@property (nonatomic, strong) MMDrawerController *drawerController;

@end

@implementation LoginViewController

- (LoginViewController *)login {
    if (!_login) {
        _login = [[LoginTranslation alloc] initWithView: self.btn];
    }
    return _login;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"登录";
}

-(void)viewWillDisappear:(BOOL)animated {
    // 反注册键盘
    [super viewWillDisappear:YES];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)loginBtn:(id)sender {
    YMSMember *member = [YMSMember loginMember];
    if (!member.token) {
        [[YMSMemberManager alloc]getTokenWithDic:nil successBlock:^(NSDictionary *result) {
          [self doRequest];
        } failureBlock:^(NSString *error) {
            
        }];
    }
    else
    {
        [self doRequest];
    }

//    // 初始化控制器
//    UIViewController *centerVc = [[CenterViewController alloc] init];
//    centerVc.transitioningDelegate = self;
//    UIViewController *leftVc = [[LeftViewController alloc] init];
//    leftVc.transitioningDelegate = self;
//    UIViewController *rightVc = [[RightViewController alloc] init];
//    rightVc.transitioningDelegate = self;
//    
//    // 初始化导航控制器
//    UINavigationController *centerNav = [[UINavigationController alloc] initWithRootViewController:centerVc];
//    UINavigationController *rightNav = [[UINavigationController alloc] initWithRootViewController:rightVc];
//    UINavigationController *leftNav = [[UINavigationController alloc] initWithRootViewController:leftVc];
//    
//    self.drawerController = [[MMDrawerController alloc] initWithCenterViewController:centerNav leftDrawerViewController:leftNav rightDrawerViewController:rightNav];
//    self.drawerController.transitioningDelegate = self;
//    
//    [self.drawerController setShowsShadow:YES];
//    [self.drawerController setMaximumLeftDrawerWidth:SCREEN_WIDTH * 0.75];
//    [self.drawerController setMaximumRightDrawerWidth:SCREEN_WIDTH * 0.75];
//    [self.drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeNone];
//    [self.drawerController setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeNone];
    //        [UIApplication sharedApplication].keyWindow.rootViewController = self.drawerController;
    
    //    RegisterViewController *registerVc = [[RegisterViewController alloc] init];
    //    registerVc.transitioningDelegate = self;
    //    [self presentViewController:registerVc animated:YES completion:nil];
//    [self presentViewController:self.drawerController animated:YES completion:nil];
    //    跟换窗口的根控制器
    
}

-(void)doRequest
{
    YMSMember *member = [YMSMember loginMember];
    if(!_passwordTextF.text.length)
    {
        NSLog(@"");
        return;
    }
    if(!_userNameTextF.text.length)
    {
        NSLog(@"");
        return;
    }
    member.rst = [NSNumber numberWithBool:YES];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:_userNameTextF.text forKey:@"username"];
    [dic setValue:_passwordTextF.text forKey:@"password"];
    [dic setValue:member.token forKey:@"_token"];
    [[YMSMemberManager alloc]MemberLoginWithDic:dic successBlock:^(NSDictionary *result){
        if (!result) {
            return ;
        }
        NSString *token = @"";
        if([YMSMember loginMember])
        {
            token = [YMSMember loginMember].token;
        }
        if ([result[@"sta"] isEqualToString:@"1"]) {
            return;
        }
        YMSMember *member = [YMSMember memberWithKeyValues:[result objectForKey:@"data"]];
        member.token = token;
        [YMSMember setLoginMember:member];
        [YMSMember saveLoginMember];
        
        if([YMSMember loginMember])
        {
            UITabBarController *tabBarVc = [[TabBarViewController alloc] init];
            //    [tabBarVc addChildViewController:self.drawerController];
            tabBarVc.transitioningDelegate = self;
            [self presentViewController:tabBarVc animated:YES completion:nil];
            
            [self performSelector:@selector(finishTransition) withObject:nil afterDelay:2];
            
        }
    } failureBlock:^(NSString *error) {
        
    }];

}
- (IBAction)RegisterBtn:(id)sender {
   RegisterViewController *registerVc = [[RegisterViewController alloc] init];
    [self presentViewController:registerVc animated:YES completion:nil];
}
#pragma mark 停止跳转动画
- (void)finishTransition {
    [self.login stopAnimation];
  
}

#pragma mark UIViewControllerTransitioningDelegate
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    self.login.reverse = YES;
    return self.login;
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    self.login.reverse = NO;
    return self.login;
}

@end


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


